﻿using Config;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using UI.TestModule;
using System.IO;
using Communication.Model.speedData;
using Communication.Model.netPacket;
using Communication.Model.packetType;
using Communication.Model.displacement;
using Communication.Model.acceleration;
using Communication.Model.configuration;
using Communication.Model.slowSignal;
using Communication.TcpIpService;
using Communication.Model.speedRealTVol;
using Communication.Interface;
using UI.Models;
using System.Threading;

namespace UI
{
    //配置文件操作类
    public partial class MainForm : Form
    {
        private void SaveSettings()
        {
            try
            {
                #region 【更新振动通道配置内容】
                //--基本信息
                int id = listBox1.SelectedIndex;
                settings.ChannelConfig.Omaplchannels.Channels[id].ChannelName = tb_name.Text;
                settings.ChannelConfig.Omaplchannels.Channels[id].FixAngle = float.Parse(tb_angle.Text);
                settings.ChannelConfig.Omaplchannels.Channels[id].SpeedNo = cb_rpm.SelectedIndex + 1;
                //--传感器信息
                settings.ChannelConfig.Omaplchannels.Channels[id].SensorType = cb_sensorType.SelectedIndex;
                settings.ChannelConfig.Omaplchannels.Channels[id].Sensitivity = float.Parse(tb_sensitivity.Text);
                settings.ChannelConfig.Omaplchannels.Channels[id].PianYiLiang = float.Parse(tb_offset.Text);
                //--报警信息
                int type = cb_sensorType.SelectedIndex;
                if (type == 0)//位移通道报警信息设置
                {
                    //更新数据
                    settings.ChannelConfig.Omaplchannels.Channels[id].Warning = float.Parse(tb_all_warning.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].Alarm = float.Parse(tb_all_alarm.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].Enable = enable_all.Checked ? 1 : 0;

                    settings.ChannelConfig.Omaplchannels.Channels[id].Frequency1Warning = float.Parse(tb_1x_warning.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].Frequency1Alarm = float.Parse(tb_1x_alarm.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].Frequency1Enable = enable_1x.Checked ? 1 : 0;

                    settings.ChannelConfig.Omaplchannels.Channels[id].Frequency2Warning = float.Parse(tb_2x_warning.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].Frequency2Alarm = float.Parse(tb_2x_alarm.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].Frequency2Enable = enable_2x.Checked ? 1 : 0;

                    settings.ChannelConfig.Omaplchannels.Channels[id].FrequencyHalfWarning = float.Parse(tb_half_warning.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].FrequencyHalfAlarm = float.Parse(tb_half_alarm.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].FrequencyHalfEnable = enable_half.Checked ? 1 : 0;
                }
                else//加速度通道报警信息设置
                {
                    settings.ChannelConfig.Omaplchannels.Channels[id].AccTotalAmplitudeYellowValue = float.Parse(tb_total_warning.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].AccTotalAmplitudeRedValue = float.Parse(tb_total_alarm.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].AccTotalAmplitudeValueIsUser = enable_total.Checked ? 1 : 0;

                    settings.ChannelConfig.Omaplchannels.Channels[id].AccLowAmplitudeYellowValue = float.Parse(tb_low_warning.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].AccLowAmplitudeRedValue = float.Parse(tb_low_alarm.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].AccLowAmplitudeValueIsUser = enable_low.Checked ? 1 : 0;

                    settings.ChannelConfig.Omaplchannels.Channels[id].AccHighAmplitudeYellowValue = float.Parse(tb_high_warning.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].AccHighAmplitudeRedValue = float.Parse(tb_high_alarm.Text);
                    settings.ChannelConfig.Omaplchannels.Channels[id].AccHighAmplitudeValueIsUser = enable_high.Checked ? 1 : 0;
                    settings.ChannelConfig.Omaplchannels.Channels[id].FlagIsUser=enable_flag.Checked ? 1 : 0;
                }

                #endregion
                //更新转速通道配置文件

                id = listBox2.SelectedIndex;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].ChannelName = tb_rpm_name.Text;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].rationdir = cb_rpm_direction.SelectedIndex;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].JianXiangCaoVolCompare = float.Parse(tb_key_voltage.Text);
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].ChiLunPanNum = int.Parse(tb_gear_num.Text);
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].ChiLunPanCompareVol = float.Parse(tb_gear_voltage.Text);
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].JianXiangCao = rb_key_manual.Checked ? 1 : 0;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].JianXiangCaoAuto = rb_key_auto.Checked ? 1 : 0;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].ChiLunPan = rb_gear_manual.Checked ? 1 : 0;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].ChiLunPanAuto = rb_gear_auto.Checked ? 1 : 0;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[id].VirtualSpeed = float.Parse(tb_rpm_virtual.Text);
                //更新采样设置
                SaveOmaplchannelsSet();
                //更新系统配置
                SaveSystemSet();
                //更新测点配置
                SaveTestPointSet();
                //更新存储配置
                SaveStorageSet();
                //保存文件
                settings.saveXml(settings.Path);


                //更新各个图
                SetGeneral();
                SetChartForm();
                SetConfigForm();
                SetDataGridForm();
                SetOctaveForm();
                //更新转速通道配置
                SetRotateForm();
                SetSelectecIndexOfListBox();

              //  MessageBox.Show("配置文件已保存，各个图已经更新");
            }
            catch (Exception ex)
            {

            }
        }
        private void SaveOmaplchannelsSet()
        {
            int index = listBox1.SelectedIndex;
            settings.ChannelConfig.Omaplchannels.Channels[index].OptionPin1Name = tb_tp_strOptionPin1Name.Text;
            settings.ChannelConfig.Omaplchannels.Channels[index].OptionPin1UpThreshold = float.Parse(tb_tp_strOptionPin1UpThreshold.Text);
            settings.ChannelConfig.Omaplchannels.Channels[index].OptionPin1DownThreshold = float.Parse(tb_tp_strOptionPin1DownThreshold.Text);
            settings.ChannelConfig.Omaplchannels.Channels[index].OptionPin2Name = tb_tp_strOptionPin2Name.Text;
            settings.ChannelConfig.Omaplchannels.Channels[index].OptionPin2UpThreshold = float.Parse(tb_tp_strOptionPin2UpThreshold.Text);
            settings.ChannelConfig.Omaplchannels.Channels[index].OptionPin2DownThreshold = float.Parse(tb_tp_strOptionPin2DownThreshold.Text);
            settings.ChannelConfig.Omaplchannels.Channels[index].OptionPin3Name = tb_tp_strOptionPin3Name.Text;
            settings.ChannelConfig.Omaplchannels.Channels[index].OptionPin3UpThreshold = float.Parse(tb_tp_strOptionPin3UpThreshold.Text);
            settings.ChannelConfig.Omaplchannels.Channels[index].OptionPin3DownThreshold = float.Parse(tb_tp_strOptionPin3DownThreshold.Text);
        }
        private void SaveStorageSet()
        {
            settings.StoreSet.HourInterval = cb_tp_nIndexHour.SelectedIndex;
            settings.StoreSet.DayInterval = cb_tp_nIndexDay.SelectedIndex;
            settings.StoreSet.WeekInterval = cb_tp_nIndexWeek.SelectedIndex;
            settings.StoreSet.MonthInterval = cb_tp_nIndexMonth.SelectedIndex;
            settings.StoreSet.YearInterval = cb_tp_nIndexYear.SelectedIndex;
            settings.StoreSet.LoadDB = int.Parse(tb_tp_strShengJiangSuBeg.Text);
            settings.StoreSet.StartSpeed = int.Parse(tb_tp_strShengJiangSuEnd.Text);
            settings.StoreSet.Store = cb_tp_nIndexVariableloadDB.SelectedIndex;
            settings.StoreSet.BlackBoxSavFre = int.Parse(tb_tp_STRHeiXiaZiBeforeEvent.Text);
            settings.StoreSet.BlackboxBeforeEvent = int.Parse(tb_tp_strHeiXiaZiAfterEvent.Text);
        }
        private void SaveSystemSet()
        {
            settings.SystemSet.IP = tb_IP.Text;
            settings.SystemSet.Port = int.Parse(tb_Port.Text);
            settings.SystemSet.IP1 = int.Parse(tb_strIP1.Text);
            settings.SystemSet.IP2 = int.Parse(tb_strIP2.Text);
            settings.SystemSet.IP3 = int.Parse(tb_strIP3.Text);
            settings.SystemSet.IP4 = int.Parse(tb_strIP4.Text);
            settings.SystemSet.nRefreshIndex = int.Parse(tb_nRefreshIndex.Text);
            settings.SystemSet.nSampleMode = cb_nSampleMode.Checked ? 1 : 0;
        }
        private void SaveTestPointSet()
        {
            int id = listBox3.SelectedIndex;
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].TestPointName = tb_tp_name.Text;
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].channelxindex = cb_tp_channelxindex.SelectedIndex;
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].channelyindex = cb_tp_channelxindey.SelectedIndex;
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].channelvola = int.Parse(tb_tp_channelvola.Text);
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].channelvolb = int.Parse(tb_tp_channelvolb.Text);
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].bearhousx = int.Parse(tb_tp_bearhousx.Text);
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].bearhousy = int.Parse(tb_tp_bearhousy.Text);
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].iniaxlex = int.Parse(tb_tp_iniaxlex.Text);
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].iniaxley = int.Parse(tb_tp_iniaxley.Text);
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].bearhori = int.Parse(tb_tp_bearhori.Text);
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].bearveri = int.Parse(tb_tp_bearveri.Text);
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].setiniposflag = cb_tp_setiniposflag.Checked ? 1 : 0;
            settings.ChannelConfig.omplconfig_testpos.TestPoints[id].saveiniposflag = cb_tp_saveiniposflag.Checked ? 1 : 0;
        }
    }
}
